//
//  UICollectionView.swift
//  rrrr
//
//  Created by A on 2025/9/28.
//

import MJRefresh
extension UICollectionView {

    /// 添加下拉刷新和上拉加载
    /// - Parameters:
    ///   - pullDownAction: 下拉刷新回调
    ///   - pullUpAction: 上拉加载回调，可选
    ///   - autoBegin: 是否自动触发首次刷新，默认为 true
    func enableRefresh(pullDownAction: @escaping () -> Void,
                       pullUpAction: (() -> Void)? = nil,
                       autoBegin: Bool = true) {
        
        // 下拉刷新
        self.mj_header = MJRefreshNormalHeader(refreshingBlock: pullDownAction)
        
        if let pullUpAction = pullUpAction {
            // 上拉加载更多
            self.mj_footer = MJRefreshAutoNormalFooter(refreshingBlock: pullUpAction)
        }
        
        // 自动触发首次刷新
        if autoBegin {
            self.mj_header?.beginRefreshing()
        }
    }
    
    /// 结束刷新
    /// - Parameters:
    ///   - noMoreData: 上拉是否显示“没有更多数据”
    func endRefreshing(noMoreData: Bool = false) {
        self.mj_header?.endRefreshing()
        if let footer = self.mj_footer {
            if noMoreData {
                footer.endRefreshingWithNoMoreData()
            } else {
                footer.endRefreshing()
            }
        }
    }
    
    /// 重置“没有更多数据”状态
    func resetNoMoreData() {
        self.mj_footer?.resetNoMoreData()
    }
}
